/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package LAgenda;

import java.io.*;
import java.util.Enumeration;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import javax.microedition.rms.*;

/**
 * @author Bruno
 */
public class LAgenda extends MIDlet implements CommandListener {

    private boolean midletPaused = false;

    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Command exitCommand;
    private Command itemOpenCamera;
    private Command backDB_camera;
    private Command itemGet;
    private Command ItemOpenInfo;
    private Command itemCommand;
    private Command backcamera_Get;
    private Command itemADD;
    private Command itemCalc;
    private Command itemSaveItem;
    private Command backinfo_Get;
    private Command itemDelDB;
    private Command itemExportDB;
    private Command itemNewDB;
    private Command itemEditDB;
    private Command itemImportDB;
    private Command itemSaveDB;
    private Command backDB_new;
    private Command backDB_EditDB;
    private Command backEditDB_EditItem;
    private Command itemDelItem;
    private Command itemEditItem;
    private Command backDB_Import;
    private Command itemHISTO;
    private Form DB;
    private StringItem SI_memory_DB;
    private ChoiceGroup CG_DB;
    private Form camera;
    private Form Get;
    private ImageItem imageGet;
    private ChoiceGroup CG_Get;
    private StringItem SI_H_Get;
    private Form info;
    private StringItem SI_About_info;
    private ImageItem imageInfo;
    private StringItem SI_name_info;
    private Form ADDItem;
    private TextField TF_name_ADD;
    private StringItem SI_rgb_ADD;
    private ImageItem imageADD;
    private TextField TF_about_ADD;
    private StringItem SI_H_ADD;
    private Form NewDB;
    private TextField TF_About_ND;
    private TextField TF_Name_ND;
    private ChoiceGroup CG_Type_ND;
    private Form EditDB;
    private ChoiceGroup CG_Itens_Edit;
    private TextField TF_about_Edit;
    private ChoiceGroup CG_Type_Edit;
    private TextField TF_name_Edit;
    private Form EditItem;
    private TextField TF_name_EI;
    private TextField TF_rgb_EI;
    private TextField TF_about_EI;
    private ImageItem imageEditItem;
    private Form importDB;
    private TextField TF_name_Import;
    private Image image1;
    //</editor-fold>//GEN-END:|fields|0|
    private String RS_NAME = "";
    private RecordStore rs;
    private Player mPlayer;
    private VideoControl mVideoControl;
    private Image image;
    private Image imageclip;
    private int x = 0;
    int[] frequencyG = new int[360];
    int id = 0;
    String frequency = "";

    /**
     * The LAgenda constructor.
     */
    public LAgenda() {
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getDB());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == ADDItem) {//GEN-BEGIN:|7-commandAction|1|114-preAction
            if (command == itemHISTO) {//GEN-END:|7-commandAction|1|114-preAction
                // write pre-action user code here
//GEN-LINE:|7-commandAction|2|114-postAction
                // write post-action user code here

                getHISTO(imageADD.getImage(),SI_H_ADD);

                SI_rgb_ADD.setText(frequency);

            } else if (command == itemSaveItem) {//GEN-LINE:|7-commandAction|3|51-preAction
                // write pre-action user code here
                switchDisplayable(null, getGet());//GEN-LINE:|7-commandAction|4|51-postAction
                // write post-action user code here

                imageclip = imageADD.getImage();
                int width = imageclip.getWidth();
                int height = imageclip.getHeight();
                int[] imgRgbData = new int[width * height];

                imageclip.getRGB(imgRgbData, 0, width, 0, 0, width, height);

                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                DataOutputStream dos = new DataOutputStream(baos);
                try
                {
                    dos.writeUTF(TF_name_ADD.getString());

                    for (int i = 0; i < imgRgbData.length; i++)
                    {
                        dos.writeInt(imgRgbData[i]);
                    }
                } catch (IOException ex) {System.out.println("IOException: "+ex);}

                add(TF_name_ADD.getString()+"\n"+SI_rgb_ADD.getText()+"\n"+new String(baos.toByteArray())+"\n"+TF_about_ADD.getString());

//                image = imageGet.getImage();
//                getHISTO(image);
                compare();
                Get.addCommand(getItemOpenInfo());

            }//GEN-BEGIN:|7-commandAction|5|19-preAction
        } else if (displayable == DB) {
            if (command == exitCommand) {//GEN-END:|7-commandAction|5|19-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|6|19-postAction
                // write post-action user code here
            } else if (command == itemDelDB) {//GEN-LINE:|7-commandAction|7|64-preAction
                // write pre-action user code here
//GEN-LINE:|7-commandAction|8|64-postAction
                // write post-action user code here

                String[] names = RecordStore.listRecordStores();
                try
                {
                    RecordStore.deleteRecordStore(names[CG_DB.getSelectedIndex()]);
                } catch( RecordStoreNotFoundException e ){System.out.println("RecordStoreNotFoundException: "+e);}
                catch( RecordStoreException e ){System.out.println("RecordStoreException: "+e);}

                analyzeAll();

            } else if (command == itemEditDB) {//GEN-LINE:|7-commandAction|9|66-preAction
                // write pre-action user code here
                switchDisplayable(null, getEditDB());//GEN-LINE:|7-commandAction|10|66-postAction
                // write post-action user code here

                RS_NAME = CG_DB.getString(CG_DB.getSelectedIndex());
                open(RS_NAME);

                TF_name_Edit.setString(getRecord(1));
                TF_about_Edit.setString(getRecord(2));
                String temp = "";
                temp = getRecord(3);
                String[] temp2 = split(temp,"\t");
                System.out.println("temp2[0]: "+temp2[0]);
                CG_Type_Edit.setSelectedIndex(Integer.parseInt(temp2[0]), true);
                additemCG(CG_Itens_Edit);

            } else if (command == itemExportDB) {//GEN-LINE:|7-commandAction|11|70-preAction
                // write pre-action user code here
//GEN-LINE:|7-commandAction|12|70-postAction
                // write post-action user code here

                Enumeration drives = FileSystemRegistry.listRoots();
                String root = (String) drives.nextElement();
                System.out.println("root: "+root);

                String where = "file:///"+root;
                String what = CG_DB.getString(CG_DB.getSelectedIndex());
                String all = where+what+".txt";
                System.out.println("export: "+all);
                export(all);

            } else if (command == itemImportDB) {//GEN-LINE:|7-commandAction|13|68-preAction
                // write pre-action user code here
                switchDisplayable(null, getImportDB());//GEN-LINE:|7-commandAction|14|68-postAction
                // write post-action user code here
            } else if (command == itemNewDB) {//GEN-LINE:|7-commandAction|15|72-preAction
                // write pre-action user code here
                switchDisplayable(null, getNewDB());//GEN-LINE:|7-commandAction|16|72-postAction
                // write post-action user code here
            } else if (command == itemOpenCamera) {//GEN-LINE:|7-commandAction|17|24-preAction
                // write pre-action user code here
//GEN-LINE:|7-commandAction|18|24-postAction
                // write post-action user code here

                //Verify if exist a database to open
                if (CG_DB.getSelectedIndex() == -1) //if not exist
                {

                } else if (CG_DB.getSelectedIndex() != -1) //if exist...
                {
                    switchDisplayable(null, getCamera());
                    RS_NAME = CG_DB.getString(CG_DB.getSelectedIndex());
                    open(RS_NAME);
                    verifyCamera();
                }

            }//GEN-BEGIN:|7-commandAction|19|86-preAction
        } else if (displayable == EditDB) {
            if (command == backDB_EditDB) {//GEN-END:|7-commandAction|19|86-preAction
                // write pre-action user code here
                switchDisplayable(null, getDB());//GEN-LINE:|7-commandAction|20|86-postAction
                // write post-action user code here

                set(TF_name_Edit.getString(),1);
                set(TF_about_Edit.getString(),2);
                close();
                analyzeAll();

            } else if (command == itemDelItem) {//GEN-LINE:|7-commandAction|21|90-preAction
                // write pre-action user code here
//GEN-LINE:|7-commandAction|22|90-postAction
                // write post-action user code here

                try
                {
                    // write pre-action user code here
                    rs.deleteRecord(CG_Itens_Edit.getSelectedIndex()+4);
                }catch (RecordStoreException e) {System.out.println("RecordStoreException: "+e);}

                additemCG(CG_Itens_Edit);

            } else if (command == itemEditItem) {//GEN-LINE:|7-commandAction|23|92-preAction
                // write pre-action user code here
                switchDisplayable(null, getEditItem());//GEN-LINE:|7-commandAction|24|92-postAction
                // write post-action user code here

                id = CG_Itens_Edit.getSelectedIndex()+4;
                System.out.println("id: "+id);
                String temp = getRecord(id);
                String[] temp2 = split(temp,"\n");
                TF_name_EI.setString(temp2[0]);
                System.out.println("temp2[1]: "+temp2[1]);
                TF_rgb_EI.setString(temp2[1]);
                System.out.println("temp2[3]: "+temp2[3]);
                TF_about_EI.setString(temp2[3]);
                getImageX(id,imageEditItem);

            }//GEN-BEGIN:|7-commandAction|25|95-preAction
        } else if (displayable == EditItem) {
            if (command == backEditDB_EditItem) {//GEN-END:|7-commandAction|25|95-preAction
                // write pre-action user code here
                switchDisplayable(null, getEditDB());//GEN-LINE:|7-commandAction|26|95-postAction
                // write post-action user code here

                //save
                int width = 100;
                int height = 100;
                int[] imgRgbData = new int[width * height];
                imageclip = imageEditItem.getImage();
                imageclip.getRGB(imgRgbData, 0, width, 0, 0, width, height);

                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                DataOutputStream dos = new DataOutputStream(baos);
                try
                {
                    dos.writeUTF(TF_name_EI.getString());

                    for (int i = 0; i < imgRgbData.length; i++)
                    {
                        dos.writeInt(imgRgbData[i]);
                    }
                } catch (IOException ex) {System.out.println("IOException: "+ex);}

                set(TF_name_EI.getString()+"\n"+TF_rgb_EI.getString()+"\n"+new String(baos.toByteArray())+"\n"+TF_about_EI.getString(),id);

                additemCG(CG_Itens_Edit);

            }//GEN-BEGIN:|7-commandAction|27|36-preAction
        } else if (displayable == Get) {
            if (command == ItemOpenInfo) {//GEN-END:|7-commandAction|27|36-preAction
                // write pre-action user code here
                switchDisplayable(null, getInfo());//GEN-LINE:|7-commandAction|28|36-postAction
                // write post-action user code here

                id = CG_Get.getSelectedIndex()+4;
                String temp = getRecord(id);
                String[] temp2 = split(temp,"\n");
                SI_name_info.setText(temp2[0]);
                SI_About_info.setText(temp2[3]);
                getImageX(id,imageInfo);

            } else if (command == backcamera_Get) {//GEN-LINE:|7-commandAction|29|38-preAction
                // write pre-action user code here

                verifyCamera();

                switchDisplayable(null, getCamera());//GEN-LINE:|7-commandAction|30|38-postAction
                // write post-action user code here
            } else if (command == itemADD) {//GEN-LINE:|7-commandAction|31|44-preAction
                // write pre-action user code here
                switchDisplayable(null, getADDItem());//GEN-LINE:|7-commandAction|32|44-postAction
                // write post-action user code here

                imageclip = imageGet.getImage();
                imageADD.setImage(imageclip);
                TF_name_ADD.setString("");
                TF_about_ADD.setString("");
                SI_rgb_ADD.setText("");

            } else if (command == itemCalc) {//GEN-LINE:|7-commandAction|33|42-preAction
                // write pre-action user code here
//GEN-LINE:|7-commandAction|34|42-postAction
                // write post-action user code here

                imageclip = imageGet.getImage();
                getHISTO(imageclip,SI_H_Get);
                compare();

            }//GEN-BEGIN:|7-commandAction|35|76-preAction
        } else if (displayable == NewDB) {
            if (command == backDB_new) {//GEN-END:|7-commandAction|35|76-preAction
                // write pre-action user code here
                switchDisplayable(null, getDB());//GEN-LINE:|7-commandAction|36|76-postAction
                // write post-action user code here
            } else if (command == itemSaveDB) {//GEN-LINE:|7-commandAction|37|79-preAction
                // write pre-action user code here
//GEN-LINE:|7-commandAction|38|79-postAction
                // write post-action user code here

                //put a name to the new database and open it
               RS_NAME = TF_Name_ND.getString();
               open(RS_NAME);
               //add a internal name
               id = 1;
               add(RS_NAME);
               //add about
               id = 2;
               add(TF_About_ND.getString());
               //add type and how many itens!
               id = 3;
               String choice = Integer.toString(CG_Type_ND.getSelectedIndex());
               try
               {
                    x = rs.getNumRecords();
               } catch (RecordStoreNotOpenException ex) {System.out.println("RecordStoreNotOpenException: "+ex);}
               String temp = String.valueOf(x);
               add(choice+"\t"+temp);
               temp = "";

               close();

               analyzeAll();

               switchDisplayable(null, getDB());

            }//GEN-BEGIN:|7-commandAction|39|27-preAction
        } else if (displayable == camera) {
            if (command == backDB_camera) {//GEN-END:|7-commandAction|39|27-preAction
                // write pre-action user code here
                switchDisplayable(null, getDB());//GEN-LINE:|7-commandAction|40|27-postAction
                // write post-action user code here

                close();
                analyzeAll();
                camera.deleteAll();

            } else if (command == itemGet) {//GEN-LINE:|7-commandAction|41|29-preAction
                // write pre-action user code here
                switchDisplayable(null, getGet());//GEN-LINE:|7-commandAction|42|29-postAction
                // write post-action user code here

                capture();
                camera.deleteAll();
                Get.removeCommand(getItemOpenInfo());
                CG_Get.deleteAll();
                additemCG(CG_Get);
                if (x>3)
                {
//                    imageclip = imageGet.getImage();
//                    getHISTO(imageclip,SI_H_Get);
//                    compare();
                    Get.addCommand(getItemOpenInfo());
                }

            }//GEN-BEGIN:|7-commandAction|43|109-preAction
        } else if (displayable == importDB) {
            if (command == backDB_Import) {//GEN-END:|7-commandAction|43|109-preAction
                // write pre-action user code here
                switchDisplayable(null, getDB());//GEN-LINE:|7-commandAction|44|109-postAction
                // write post-action user code here

                Enumeration drives = FileSystemRegistry.listRoots();
                String root = (String) drives.nextElement();
                System.out.println("root: "+root);

                String where = "file:///"+root;
                String what = TF_name_Import.getString();

                try
                {
                    importfile(where+what+".txt");
                } catch (IOException ex) {System.out.println("IOException: "+ex);}

                analyzeAll();

            }//GEN-BEGIN:|7-commandAction|45|49-preAction
        } else if (displayable == info) {
            if (command == backinfo_Get) {//GEN-END:|7-commandAction|45|49-preAction
                // write pre-action user code here
                switchDisplayable(null, getGet());//GEN-LINE:|7-commandAction|46|49-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|47|7-postCommandAction
        }//GEN-END:|7-commandAction|47|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|48|
    //</editor-fold>//GEN-END:|7-commandAction|48|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|18-getter|0|18-preInit
    /**
     * Returns an initiliazed instance of exitCommand component.
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {//GEN-END:|18-getter|0|18-preInit
            // write pre-init user code here
            exitCommand = new Command(LocalizationSupport.getMessage("exit"), Command.EXIT, 0);//GEN-LINE:|18-getter|1|18-postInit
            // write post-init user code here
        }//GEN-BEGIN:|18-getter|2|
        return exitCommand;
    }
    //</editor-fold>//GEN-END:|18-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: DB ">//GEN-BEGIN:|14-getter|0|14-preInit
    /**
     * Returns an initiliazed instance of DB component.
     * @return the initialized component instance
     */
    public Form getDB() {
        if (DB == null) {//GEN-END:|14-getter|0|14-preInit
            // write pre-init user code here
            DB = new Form("LAgenda", new Item[] { getSI_memory_DB(), getCG_DB() });//GEN-BEGIN:|14-getter|1|14-postInit
            DB.addCommand(getItemOpenCamera());
            DB.addCommand(getItemDelDB());
            DB.addCommand(getItemEditDB());
            DB.addCommand(getItemImportDB());
            DB.addCommand(getItemExportDB());
            DB.addCommand(getItemNewDB());
            DB.addCommand(getExitCommand());
            DB.setCommandListener(this);//GEN-END:|14-getter|1|14-postInit
            // write post-init user code here

            analyzeAll();

        }//GEN-BEGIN:|14-getter|2|
        return DB;
    }
    //</editor-fold>//GEN-END:|14-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: SI_memory_DB ">//GEN-BEGIN:|16-getter|0|16-preInit
    /**
     * Returns an initiliazed instance of SI_memory_DB component.
     * @return the initialized component instance
     */
    public StringItem getSI_memory_DB() {
        if (SI_memory_DB == null) {//GEN-END:|16-getter|0|16-preInit
            // write pre-init user code here
            SI_memory_DB = new StringItem(LocalizationSupport.getMessage("free_memory"), "");//GEN-LINE:|16-getter|1|16-postInit
            // write post-init user code here
        }//GEN-BEGIN:|16-getter|2|
        return SI_memory_DB;
    }
    //</editor-fold>//GEN-END:|16-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: CG_DB ">//GEN-BEGIN:|22-getter|0|22-preInit
    /**
     * Returns an initiliazed instance of CG_DB component.
     * @return the initialized component instance
     */
    public ChoiceGroup getCG_DB() {
        if (CG_DB == null) {//GEN-END:|22-getter|0|22-preInit
            // write pre-init user code here
            CG_DB = new ChoiceGroup(LocalizationSupport.getMessage("database"), Choice.EXCLUSIVE);//GEN-LINE:|22-getter|1|22-postInit
            // write post-init user code here
        }//GEN-BEGIN:|22-getter|2|
        return CG_DB;
    }
    //</editor-fold>//GEN-END:|22-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemOpenCamera ">//GEN-BEGIN:|23-getter|0|23-preInit
    /**
     * Returns an initiliazed instance of itemOpenCamera component.
     * @return the initialized component instance
     */
    public Command getItemOpenCamera() {
        if (itemOpenCamera == null) {//GEN-END:|23-getter|0|23-preInit
            // write pre-init user code here
            itemOpenCamera = new Command(LocalizationSupport.getMessage("open"), Command.ITEM, 0);//GEN-LINE:|23-getter|1|23-postInit
            // write post-init user code here
        }//GEN-BEGIN:|23-getter|2|
        return itemOpenCamera;
    }
    //</editor-fold>//GEN-END:|23-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: camera ">//GEN-BEGIN:|25-getter|0|25-preInit
    /**
     * Returns an initiliazed instance of camera component.
     * @return the initialized component instance
     */
    public Form getCamera() {
        if (camera == null) {//GEN-END:|25-getter|0|25-preInit
            // write pre-init user code here
            camera = new Form(LocalizationSupport.getMessage("camera"));//GEN-BEGIN:|25-getter|1|25-postInit
            camera.addCommand(getItemGet());
            camera.addCommand(getBackDB_camera());
            camera.setCommandListener(this);//GEN-END:|25-getter|1|25-postInit
            // write post-init user code here
        }//GEN-BEGIN:|25-getter|2|
        return camera;
    }
    //</editor-fold>//GEN-END:|25-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backDB_camera ">//GEN-BEGIN:|26-getter|0|26-preInit
    /**
     * Returns an initiliazed instance of backDB_camera component.
     * @return the initialized component instance
     */
    public Command getBackDB_camera() {
        if (backDB_camera == null) {//GEN-END:|26-getter|0|26-preInit
            // write pre-init user code here
            backDB_camera = new Command(LocalizationSupport.getMessage("back"), Command.BACK, 0);//GEN-LINE:|26-getter|1|26-postInit
            // write post-init user code here
        }//GEN-BEGIN:|26-getter|2|
        return backDB_camera;
    }
    //</editor-fold>//GEN-END:|26-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemGet ">//GEN-BEGIN:|28-getter|0|28-preInit
    /**
     * Returns an initiliazed instance of itemGet component.
     * @return the initialized component instance
     */
    public Command getItemGet() {
        if (itemGet == null) {//GEN-END:|28-getter|0|28-preInit
            // write pre-init user code here
            itemGet = new Command(LocalizationSupport.getMessage("get"), Command.ITEM, 0);//GEN-LINE:|28-getter|1|28-postInit
            // write post-init user code here
        }//GEN-BEGIN:|28-getter|2|
        return itemGet;
    }
    //</editor-fold>//GEN-END:|28-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Get ">//GEN-BEGIN:|30-getter|0|30-preInit
    /**
     * Returns an initiliazed instance of Get component.
     * @return the initialized component instance
     */
    public Form getGet() {
        if (Get == null) {//GEN-END:|30-getter|0|30-preInit
            // write pre-init user code here
            Get = new Form("", new Item[] { getSI_H_Get(), getImageGet(), getCG_Get() });//GEN-BEGIN:|30-getter|1|30-postInit
            Get.addCommand(getItemCalc());
            Get.addCommand(getItemADD());
            Get.addCommand(getItemOpenInfo());
            Get.addCommand(getBackcamera_Get());
            Get.setCommandListener(this);//GEN-END:|30-getter|1|30-postInit
            // write post-init user code here
        }//GEN-BEGIN:|30-getter|2|
        return Get;
    }
    //</editor-fold>//GEN-END:|30-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: imageGet ">//GEN-BEGIN:|32-getter|0|32-preInit
    /**
     * Returns an initiliazed instance of imageGet component.
     * @return the initialized component instance
     */
    public ImageItem getImageGet() {
        if (imageGet == null) {//GEN-END:|32-getter|0|32-preInit
            // write pre-init user code here
            imageGet = new ImageItem("", getImage1(), ImageItem.LAYOUT_DEFAULT, "<Imagem perdida>");//GEN-LINE:|32-getter|1|32-postInit
            // write post-init user code here
        }//GEN-BEGIN:|32-getter|2|
        return imageGet;
    }
    //</editor-fold>//GEN-END:|32-getter|2|



    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: image1 ">//GEN-BEGIN:|34-getter|0|34-preInit
    /**
     * Returns an initiliazed instance of image1 component.
     * @return the initialized component instance
     */
    public Image getImage1() {
        if (image1 == null) {//GEN-END:|34-getter|0|34-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|34-getter|1|34-@java.io.IOException
                image1 = Image.createImage("/1.PNG");
            } catch (java.io.IOException e) {//GEN-END:|34-getter|1|34-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|34-getter|2|34-postInit
            // write post-init user code here
        }//GEN-BEGIN:|34-getter|3|
        return image1;
    }
    //</editor-fold>//GEN-END:|34-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: ItemOpenInfo ">//GEN-BEGIN:|35-getter|0|35-preInit
    /**
     * Returns an initiliazed instance of ItemOpenInfo component.
     * @return the initialized component instance
     */
    public Command getItemOpenInfo() {
        if (ItemOpenInfo == null) {//GEN-END:|35-getter|0|35-preInit
            // write pre-init user code here
            ItemOpenInfo = new Command(LocalizationSupport.getMessage("info"), Command.ITEM, 0);//GEN-LINE:|35-getter|1|35-postInit
            // write post-init user code here
        }//GEN-BEGIN:|35-getter|2|
        return ItemOpenInfo;
    }
    //</editor-fold>//GEN-END:|35-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backcamera_Get ">//GEN-BEGIN:|37-getter|0|37-preInit
    /**
     * Returns an initiliazed instance of backcamera_Get component.
     * @return the initialized component instance
     */
    public Command getBackcamera_Get() {
        if (backcamera_Get == null) {//GEN-END:|37-getter|0|37-preInit
            // write pre-init user code here
            backcamera_Get = new Command(LocalizationSupport.getMessage("back"), Command.BACK, 0);//GEN-LINE:|37-getter|1|37-postInit
            // write post-init user code here
        }//GEN-BEGIN:|37-getter|2|
        return backcamera_Get;
    }
    //</editor-fold>//GEN-END:|37-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemCommand ">//GEN-BEGIN:|39-getter|0|39-preInit
    /**
     * Returns an initiliazed instance of itemCommand component.
     * @return the initialized component instance
     */
    public Command getItemCommand() {
        if (itemCommand == null) {//GEN-END:|39-getter|0|39-preInit
            // write pre-init user code here
            itemCommand = new Command("Item", Command.ITEM, 0);//GEN-LINE:|39-getter|1|39-postInit
            // write post-init user code here
        }//GEN-BEGIN:|39-getter|2|
        return itemCommand;
    }
    //</editor-fold>//GEN-END:|39-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemCalc ">//GEN-BEGIN:|41-getter|0|41-preInit
    /**
     * Returns an initiliazed instance of itemCalc component.
     * @return the initialized component instance
     */
    public Command getItemCalc() {
        if (itemCalc == null) {//GEN-END:|41-getter|0|41-preInit
            // write pre-init user code here
            itemCalc = new Command(LocalizationSupport.getMessage("calc"), Command.ITEM, 0);//GEN-LINE:|41-getter|1|41-postInit
            // write post-init user code here
        }//GEN-BEGIN:|41-getter|2|
        return itemCalc;
    }
    //</editor-fold>//GEN-END:|41-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemADD ">//GEN-BEGIN:|43-getter|0|43-preInit
    /**
     * Returns an initiliazed instance of itemADD component.
     * @return the initialized component instance
     */
    public Command getItemADD() {
        if (itemADD == null) {//GEN-END:|43-getter|0|43-preInit
            // write pre-init user code here
            itemADD = new Command(LocalizationSupport.getMessage("add"), Command.ITEM, 0);//GEN-LINE:|43-getter|1|43-postInit
            // write post-init user code here
        }//GEN-BEGIN:|43-getter|2|
        return itemADD;
    }
    //</editor-fold>//GEN-END:|43-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: CG_Get ">//GEN-BEGIN:|45-getter|0|45-preInit
    /**
     * Returns an initiliazed instance of CG_Get component.
     * @return the initialized component instance
     */
    public ChoiceGroup getCG_Get() {
        if (CG_Get == null) {//GEN-END:|45-getter|0|45-preInit
            // write pre-init user code here
            CG_Get = new ChoiceGroup(LocalizationSupport.getMessage("calc"), Choice.EXCLUSIVE);//GEN-LINE:|45-getter|1|45-postInit
            // write post-init user code here
        }//GEN-BEGIN:|45-getter|2|
        return CG_Get;
    }
    //</editor-fold>//GEN-END:|45-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backinfo_Get ">//GEN-BEGIN:|48-getter|0|48-preInit
    /**
     * Returns an initiliazed instance of backinfo_Get component.
     * @return the initialized component instance
     */
    public Command getBackinfo_Get() {
        if (backinfo_Get == null) {//GEN-END:|48-getter|0|48-preInit
            // write pre-init user code here
            backinfo_Get = new Command(LocalizationSupport.getMessage("back"), Command.BACK, 0);//GEN-LINE:|48-getter|1|48-postInit
            // write post-init user code here
        }//GEN-BEGIN:|48-getter|2|
        return backinfo_Get;
    }
    //</editor-fold>//GEN-END:|48-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemSaveItem ">//GEN-BEGIN:|50-getter|0|50-preInit
    /**
     * Returns an initiliazed instance of itemSaveItem component.
     * @return the initialized component instance
     */
    public Command getItemSaveItem() {
        if (itemSaveItem == null) {//GEN-END:|50-getter|0|50-preInit
            // write pre-init user code here
            itemSaveItem = new Command(LocalizationSupport.getMessage("save"), Command.BACK, 0);//GEN-LINE:|50-getter|1|50-postInit
            // write post-init user code here
        }//GEN-BEGIN:|50-getter|2|
        return itemSaveItem;
    }
    //</editor-fold>//GEN-END:|50-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: info ">//GEN-BEGIN:|46-getter|0|46-preInit
    /**
     * Returns an initiliazed instance of info component.
     * @return the initialized component instance
     */
    public Form getInfo() {
        if (info == null) {//GEN-END:|46-getter|0|46-preInit
            // write pre-init user code here
            info = new Form(LocalizationSupport.getMessage("info"), new Item[] { getSI_name_info(), getImageInfo(), getSI_About_info() });//GEN-BEGIN:|46-getter|1|46-postInit
            info.addCommand(getBackinfo_Get());
            info.setCommandListener(this);//GEN-END:|46-getter|1|46-postInit
            // write post-init user code here
        }//GEN-BEGIN:|46-getter|2|
        return info;
    }
    //</editor-fold>//GEN-END:|46-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: ADDItem ">//GEN-BEGIN:|47-getter|0|47-preInit
    /**
     * Returns an initiliazed instance of ADDItem component.
     * @return the initialized component instance
     */
    public Form getADDItem() {
        if (ADDItem == null) {//GEN-END:|47-getter|0|47-preInit
            // write pre-init user code here
            ADDItem = new Form(LocalizationSupport.getMessage("add"), new Item[] { getSI_H_ADD(), getTF_name_ADD(), getTF_about_ADD(), getImageADD(), getSI_rgb_ADD() });//GEN-BEGIN:|47-getter|1|47-postInit
            ADDItem.addCommand(getItemSaveItem());
            ADDItem.addCommand(getItemHISTO());
            ADDItem.setCommandListener(this);//GEN-END:|47-getter|1|47-postInit
            // write post-init user code here


//            getHISTO(imageclip);
//            for (int i = 0; i<360;i++)
//            {
//                frequency = frequency + String.valueOf(frequencyG[i]) + ";";
//            }
//            SI_rgb_ADD.setText(frequency);
            //compare();

        }//GEN-BEGIN:|47-getter|2|
        return ADDItem;
    }
    //</editor-fold>//GEN-END:|47-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: SI_name_info ">//GEN-BEGIN:|56-getter|0|56-preInit
    /**
     * Returns an initiliazed instance of SI_name_info component.
     * @return the initialized component instance
     */
    public StringItem getSI_name_info() {
        if (SI_name_info == null) {//GEN-END:|56-getter|0|56-preInit
            // write pre-init user code here
            SI_name_info = new StringItem(LocalizationSupport.getMessage("name"), "");//GEN-LINE:|56-getter|1|56-postInit
            // write post-init user code here
        }//GEN-BEGIN:|56-getter|2|
        return SI_name_info;
    }
    //</editor-fold>//GEN-END:|56-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: imageInfo ">//GEN-BEGIN:|57-getter|0|57-preInit
    /**
     * Returns an initiliazed instance of imageInfo component.
     * @return the initialized component instance
     */
    public ImageItem getImageInfo() {
        if (imageInfo == null) {//GEN-END:|57-getter|0|57-preInit
            // write pre-init user code here
            imageInfo = new ImageItem("", getImage1(), ImageItem.LAYOUT_DEFAULT, "<Imagem perdida>");//GEN-LINE:|57-getter|1|57-postInit
            // write post-init user code here
        }//GEN-BEGIN:|57-getter|2|
        return imageInfo;
    }
    //</editor-fold>//GEN-END:|57-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: SI_About_info ">//GEN-BEGIN:|58-getter|0|58-preInit
    /**
     * Returns an initiliazed instance of SI_About_info component.
     * @return the initialized component instance
     */
    public StringItem getSI_About_info() {
        if (SI_About_info == null) {//GEN-END:|58-getter|0|58-preInit
            // write pre-init user code here
            SI_About_info = new StringItem(LocalizationSupport.getMessage("about"), "");//GEN-LINE:|58-getter|1|58-postInit
            // write post-init user code here
        }//GEN-BEGIN:|58-getter|2|
        return SI_About_info;
    }
    //</editor-fold>//GEN-END:|58-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: TF_name_ADD ">//GEN-BEGIN:|59-getter|0|59-preInit
    /**
     * Returns an initiliazed instance of TF_name_ADD component.
     * @return the initialized component instance
     */
    public TextField getTF_name_ADD() {
        if (TF_name_ADD == null) {//GEN-END:|59-getter|0|59-preInit
            // write pre-init user code here
            TF_name_ADD = new TextField(LocalizationSupport.getMessage("name"), "", 32, TextField.ANY);//GEN-LINE:|59-getter|1|59-postInit
            // write post-init user code here
        }//GEN-BEGIN:|59-getter|2|
        return TF_name_ADD;
    }
    //</editor-fold>//GEN-END:|59-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: TF_about_ADD ">//GEN-BEGIN:|60-getter|0|60-preInit
    /**
     * Returns an initiliazed instance of TF_about_ADD component.
     * @return the initialized component instance
     */
    public TextField getTF_about_ADD() {
        if (TF_about_ADD == null) {//GEN-END:|60-getter|0|60-preInit
            // write pre-init user code here
            TF_about_ADD = new TextField(LocalizationSupport.getMessage("about"), "", 32, TextField.ANY);//GEN-LINE:|60-getter|1|60-postInit
            // write post-init user code here
        }//GEN-BEGIN:|60-getter|2|
        return TF_about_ADD;
    }
    //</editor-fold>//GEN-END:|60-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: imageADD ">//GEN-BEGIN:|61-getter|0|61-preInit
    /**
     * Returns an initiliazed instance of imageADD component.
     * @return the initialized component instance
     */
    public ImageItem getImageADD() {
        if (imageADD == null) {//GEN-END:|61-getter|0|61-preInit
            // write pre-init user code here
            imageADD = new ImageItem("", getImage1(), ImageItem.LAYOUT_DEFAULT, "<Imagem perdida>");//GEN-LINE:|61-getter|1|61-postInit
            // write post-init user code here
        }//GEN-BEGIN:|61-getter|2|
        return imageADD;
    }
    //</editor-fold>//GEN-END:|61-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: SI_rgb_ADD ">//GEN-BEGIN:|62-getter|0|62-preInit
    /**
     * Returns an initiliazed instance of SI_rgb_ADD component.
     * @return the initialized component instance
     */
    public StringItem getSI_rgb_ADD() {
        if (SI_rgb_ADD == null) {//GEN-END:|62-getter|0|62-preInit
            // write pre-init user code here
            SI_rgb_ADD = new StringItem("RGB:", "");//GEN-LINE:|62-getter|1|62-postInit
            // write post-init user code here
        }//GEN-BEGIN:|62-getter|2|
        return SI_rgb_ADD;
    }
    //</editor-fold>//GEN-END:|62-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemDelDB ">//GEN-BEGIN:|63-getter|0|63-preInit
    /**
     * Returns an initiliazed instance of itemDelDB component.
     * @return the initialized component instance
     */
    public Command getItemDelDB() {
        if (itemDelDB == null) {//GEN-END:|63-getter|0|63-preInit
            // write pre-init user code here
            itemDelDB = new Command(LocalizationSupport.getMessage("del"), Command.ITEM, 0);//GEN-LINE:|63-getter|1|63-postInit
            // write post-init user code here
        }//GEN-BEGIN:|63-getter|2|
        return itemDelDB;
    }
    //</editor-fold>//GEN-END:|63-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemEditDB ">//GEN-BEGIN:|65-getter|0|65-preInit
    /**
     * Returns an initiliazed instance of itemEditDB component.
     * @return the initialized component instance
     */
    public Command getItemEditDB() {
        if (itemEditDB == null) {//GEN-END:|65-getter|0|65-preInit
            // write pre-init user code here
            itemEditDB = new Command(LocalizationSupport.getMessage("edit"), Command.ITEM, 0);//GEN-LINE:|65-getter|1|65-postInit
            // write post-init user code here
        }//GEN-BEGIN:|65-getter|2|
        return itemEditDB;
    }
    //</editor-fold>//GEN-END:|65-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemImportDB ">//GEN-BEGIN:|67-getter|0|67-preInit
    /**
     * Returns an initiliazed instance of itemImportDB component.
     * @return the initialized component instance
     */
    public Command getItemImportDB() {
        if (itemImportDB == null) {//GEN-END:|67-getter|0|67-preInit
            // write pre-init user code here
            itemImportDB = new Command(LocalizationSupport.getMessage("import"), Command.ITEM, 0);//GEN-LINE:|67-getter|1|67-postInit
            // write post-init user code here
        }//GEN-BEGIN:|67-getter|2|
        return itemImportDB;
    }
    //</editor-fold>//GEN-END:|67-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemExportDB ">//GEN-BEGIN:|69-getter|0|69-preInit
    /**
     * Returns an initiliazed instance of itemExportDB component.
     * @return the initialized component instance
     */
    public Command getItemExportDB() {
        if (itemExportDB == null) {//GEN-END:|69-getter|0|69-preInit
            // write pre-init user code here
            itemExportDB = new Command(LocalizationSupport.getMessage("export"), Command.ITEM, 0);//GEN-LINE:|69-getter|1|69-postInit
            // write post-init user code here
        }//GEN-BEGIN:|69-getter|2|
        return itemExportDB;
    }
    //</editor-fold>//GEN-END:|69-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemNewDB ">//GEN-BEGIN:|71-getter|0|71-preInit
    /**
     * Returns an initiliazed instance of itemNewDB component.
     * @return the initialized component instance
     */
    public Command getItemNewDB() {
        if (itemNewDB == null) {//GEN-END:|71-getter|0|71-preInit
            // write pre-init user code here
            itemNewDB = new Command(LocalizationSupport.getMessage("new"), Command.ITEM, 0);//GEN-LINE:|71-getter|1|71-postInit
            // write post-init user code here
        }//GEN-BEGIN:|71-getter|2|
        return itemNewDB;
    }
    //</editor-fold>//GEN-END:|71-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backDB_new ">//GEN-BEGIN:|75-getter|0|75-preInit
    /**
     * Returns an initiliazed instance of backDB_new component.
     * @return the initialized component instance
     */
    public Command getBackDB_new() {
        if (backDB_new == null) {//GEN-END:|75-getter|0|75-preInit
            // write pre-init user code here
            backDB_new = new Command(LocalizationSupport.getMessage("back"), Command.BACK, 0);//GEN-LINE:|75-getter|1|75-postInit
            // write post-init user code here
        }//GEN-BEGIN:|75-getter|2|
        return backDB_new;
    }
    //</editor-fold>//GEN-END:|75-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: NewDB ">//GEN-BEGIN:|73-getter|0|73-preInit
    /**
     * Returns an initiliazed instance of NewDB component.
     * @return the initialized component instance
     */
    public Form getNewDB() {
        if (NewDB == null) {//GEN-END:|73-getter|0|73-preInit
            // write pre-init user code here
            NewDB = new Form(LocalizationSupport.getMessage("newDB"), new Item[] { getTF_Name_ND(), getTF_About_ND(), getCG_Type_ND() });//GEN-BEGIN:|73-getter|1|73-postInit
            NewDB.addCommand(getItemSaveDB());
            NewDB.addCommand(getBackDB_new());
            NewDB.setCommandListener(this);//GEN-END:|73-getter|1|73-postInit
            // write post-init user code here
        }//GEN-BEGIN:|73-getter|2|
        return NewDB;
    }
    //</editor-fold>//GEN-END:|73-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemSaveDB ">//GEN-BEGIN:|78-getter|0|78-preInit
    /**
     * Returns an initiliazed instance of itemSaveDB component.
     * @return the initialized component instance
     */
    public Command getItemSaveDB() {
        if (itemSaveDB == null) {//GEN-END:|78-getter|0|78-preInit
            // write pre-init user code here
            itemSaveDB = new Command(LocalizationSupport.getMessage("save"), Command.ITEM, 0);//GEN-LINE:|78-getter|1|78-postInit
            // write post-init user code here
        }//GEN-BEGIN:|78-getter|2|
        return itemSaveDB;
    }
    //</editor-fold>//GEN-END:|78-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: TF_Name_ND ">//GEN-BEGIN:|80-getter|0|80-preInit
    /**
     * Returns an initiliazed instance of TF_Name_ND component.
     * @return the initialized component instance
     */
    public TextField getTF_Name_ND() {
        if (TF_Name_ND == null) {//GEN-END:|80-getter|0|80-preInit
            // write pre-init user code here
            TF_Name_ND = new TextField(LocalizationSupport.getMessage("name"), "", 32, TextField.ANY);//GEN-LINE:|80-getter|1|80-postInit
            // write post-init user code here
        }//GEN-BEGIN:|80-getter|2|
        return TF_Name_ND;
    }
    //</editor-fold>//GEN-END:|80-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: TF_About_ND ">//GEN-BEGIN:|81-getter|0|81-preInit
    /**
     * Returns an initiliazed instance of TF_About_ND component.
     * @return the initialized component instance
     */
    public TextField getTF_About_ND() {
        if (TF_About_ND == null) {//GEN-END:|81-getter|0|81-preInit
            // write pre-init user code here
            TF_About_ND = new TextField(LocalizationSupport.getMessage("about"), "", 32, TextField.ANY);//GEN-LINE:|81-getter|1|81-postInit
            // write post-init user code here
        }//GEN-BEGIN:|81-getter|2|
        return TF_About_ND;
    }
    //</editor-fold>//GEN-END:|81-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: CG_Type_ND ">//GEN-BEGIN:|82-getter|0|82-preInit
    /**
     * Returns an initiliazed instance of CG_Type_ND component.
     * @return the initialized component instance
     */
    public ChoiceGroup getCG_Type_ND() {
        if (CG_Type_ND == null) {//GEN-END:|82-getter|0|82-preInit
            // write pre-init user code here
            CG_Type_ND = new ChoiceGroup(LocalizationSupport.getMessage("type"), Choice.EXCLUSIVE);//GEN-BEGIN:|82-getter|1|82-postInit
            CG_Type_ND.setFitPolicy(Choice.TEXT_WRAP_DEFAULT);//GEN-END:|82-getter|1|82-postInit
            // write post-init user code here

            CG_Type_ND.append(LocalizationSupport.getMessage("color"),null);
            CG_Type_ND.append(LocalizationSupport.getMessage("colorshape"),null);
            CG_Type_ND.setSelectedIndex(0, true);

        }//GEN-BEGIN:|82-getter|2|
        return CG_Type_ND;
    }
    //</editor-fold>//GEN-END:|82-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backDB_EditDB ">//GEN-BEGIN:|85-getter|0|85-preInit
    /**
     * Returns an initiliazed instance of backDB_EditDB component.
     * @return the initialized component instance
     */
    public Command getBackDB_EditDB() {
        if (backDB_EditDB == null) {//GEN-END:|85-getter|0|85-preInit
            // write pre-init user code here
            backDB_EditDB = new Command(LocalizationSupport.getMessage("saveback"), Command.BACK, 0);//GEN-LINE:|85-getter|1|85-postInit
            // write post-init user code here
        }//GEN-BEGIN:|85-getter|2|
        return backDB_EditDB;
    }
    //</editor-fold>//GEN-END:|85-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemDelItem ">//GEN-BEGIN:|89-getter|0|89-preInit
    /**
     * Returns an initiliazed instance of itemDelItem component.
     * @return the initialized component instance
     */
    public Command getItemDelItem() {
        if (itemDelItem == null) {//GEN-END:|89-getter|0|89-preInit
            // write pre-init user code here
            itemDelItem = new Command(LocalizationSupport.getMessage("delitem"), Command.ITEM, 0);//GEN-LINE:|89-getter|1|89-postInit
            // write post-init user code here
        }//GEN-BEGIN:|89-getter|2|
        return itemDelItem;
    }
    //</editor-fold>//GEN-END:|89-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemEditItem ">//GEN-BEGIN:|91-getter|0|91-preInit
    /**
     * Returns an initiliazed instance of itemEditItem component.
     * @return the initialized component instance
     */
    public Command getItemEditItem() {
        if (itemEditItem == null) {//GEN-END:|91-getter|0|91-preInit
            // write pre-init user code here
            itemEditItem = new Command(LocalizationSupport.getMessage("edititem"), Command.ITEM, 0);//GEN-LINE:|91-getter|1|91-postInit
            // write post-init user code here
        }//GEN-BEGIN:|91-getter|2|
        return itemEditItem;
    }
    //</editor-fold>//GEN-END:|91-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: EditDB ">//GEN-BEGIN:|83-getter|0|83-preInit
    /**
     * Returns an initiliazed instance of EditDB component.
     * @return the initialized component instance
     */
    public Form getEditDB() {
        if (EditDB == null) {//GEN-END:|83-getter|0|83-preInit
            // write pre-init user code here
            EditDB = new Form(LocalizationSupport.getMessage("edit"), new Item[] { getTF_name_Edit(), getTF_about_Edit(), getCG_Type_Edit(), getCG_Itens_Edit() });//GEN-BEGIN:|83-getter|1|83-postInit
            EditDB.addCommand(getItemDelItem());
            EditDB.addCommand(getItemEditItem());
            EditDB.addCommand(getBackDB_EditDB());
            EditDB.setCommandListener(this);//GEN-END:|83-getter|1|83-postInit
            // write post-init user code here
        }//GEN-BEGIN:|83-getter|2|
        return EditDB;
    }
    //</editor-fold>//GEN-END:|83-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: EditItem ">//GEN-BEGIN:|88-getter|0|88-preInit
    /**
     * Returns an initiliazed instance of EditItem component.
     * @return the initialized component instance
     */
    public Form getEditItem() {
        if (EditItem == null) {//GEN-END:|88-getter|0|88-preInit
            // write pre-init user code here
            EditItem = new Form(LocalizationSupport.getMessage("edit"), new Item[] { getTF_name_EI(), getTF_about_EI(), getImageEditItem(), getTF_rgb_EI() });//GEN-BEGIN:|88-getter|1|88-postInit
            EditItem.addCommand(getBackEditDB_EditItem());
            EditItem.setCommandListener(this);//GEN-END:|88-getter|1|88-postInit
            // write post-init user code here
        }//GEN-BEGIN:|88-getter|2|
        return EditItem;
    }
    //</editor-fold>//GEN-END:|88-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backEditDB_EditItem ">//GEN-BEGIN:|94-getter|0|94-preInit
    /**
     * Returns an initiliazed instance of backEditDB_EditItem component.
     * @return the initialized component instance
     */
    public Command getBackEditDB_EditItem() {
        if (backEditDB_EditItem == null) {//GEN-END:|94-getter|0|94-preInit
            // write pre-init user code here
            backEditDB_EditItem = new Command(LocalizationSupport.getMessage("saveback"), Command.ITEM, 0);//GEN-LINE:|94-getter|1|94-postInit
            // write post-init user code here
        }//GEN-BEGIN:|94-getter|2|
        return backEditDB_EditItem;
    }
    //</editor-fold>//GEN-END:|94-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: TF_name_Edit ">//GEN-BEGIN:|97-getter|0|97-preInit
    /**
     * Returns an initiliazed instance of TF_name_Edit component.
     * @return the initialized component instance
     */
    public TextField getTF_name_Edit() {
        if (TF_name_Edit == null) {//GEN-END:|97-getter|0|97-preInit
            // write pre-init user code here
            TF_name_Edit = new TextField(LocalizationSupport.getMessage("name"), "", 32, TextField.ANY);//GEN-LINE:|97-getter|1|97-postInit
            // write post-init user code here
        }//GEN-BEGIN:|97-getter|2|
        return TF_name_Edit;
    }
    //</editor-fold>//GEN-END:|97-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: TF_about_Edit ">//GEN-BEGIN:|98-getter|0|98-preInit
    /**
     * Returns an initiliazed instance of TF_about_Edit component.
     * @return the initialized component instance
     */
    public TextField getTF_about_Edit() {
        if (TF_about_Edit == null) {//GEN-END:|98-getter|0|98-preInit
            // write pre-init user code here
            TF_about_Edit = new TextField(LocalizationSupport.getMessage("about"), "", 32, TextField.ANY);//GEN-LINE:|98-getter|1|98-postInit
            // write post-init user code here
        }//GEN-BEGIN:|98-getter|2|
        return TF_about_Edit;
    }
    //</editor-fold>//GEN-END:|98-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: CG_Type_Edit ">//GEN-BEGIN:|99-getter|0|99-preInit
    /**
     * Returns an initiliazed instance of CG_Type_Edit component.
     * @return the initialized component instance
     */
    public ChoiceGroup getCG_Type_Edit() {
        if (CG_Type_Edit == null) {//GEN-END:|99-getter|0|99-preInit
            // write pre-init user code here
            CG_Type_Edit = new ChoiceGroup(LocalizationSupport.getMessage("type"), Choice.EXCLUSIVE);//GEN-LINE:|99-getter|1|99-postInit
            // write post-init user code here

            CG_Type_Edit.append(LocalizationSupport.getMessage("color"),null);
            CG_Type_Edit.append(LocalizationSupport.getMessage("colorshape"),null);


        }//GEN-BEGIN:|99-getter|2|
        return CG_Type_Edit;
    }
    //</editor-fold>//GEN-END:|99-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: CG_Itens_Edit ">//GEN-BEGIN:|100-getter|0|100-preInit
    /**
     * Returns an initiliazed instance of CG_Itens_Edit component.
     * @return the initialized component instance
     */
    public ChoiceGroup getCG_Itens_Edit() {
        if (CG_Itens_Edit == null) {//GEN-END:|100-getter|0|100-preInit
            // write pre-init user code here
            CG_Itens_Edit = new ChoiceGroup(LocalizationSupport.getMessage("items"), Choice.EXCLUSIVE);//GEN-LINE:|100-getter|1|100-postInit
            // write post-init user code here
        }//GEN-BEGIN:|100-getter|2|
        return CG_Itens_Edit;
    }
    //</editor-fold>//GEN-END:|100-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: TF_name_EI ">//GEN-BEGIN:|103-getter|0|103-preInit
    /**
     * Returns an initiliazed instance of TF_name_EI component.
     * @return the initialized component instance
     */
    public TextField getTF_name_EI() {
        if (TF_name_EI == null) {//GEN-END:|103-getter|0|103-preInit
            // write pre-init user code here
            TF_name_EI = new TextField(LocalizationSupport.getMessage("name"), "", 32, TextField.ANY);//GEN-LINE:|103-getter|1|103-postInit
            // write post-init user code here
        }//GEN-BEGIN:|103-getter|2|
        return TF_name_EI;
    }
    //</editor-fold>//GEN-END:|103-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: TF_about_EI ">//GEN-BEGIN:|104-getter|0|104-preInit
    /**
     * Returns an initiliazed instance of TF_about_EI component.
     * @return the initialized component instance
     */
    public TextField getTF_about_EI() {
        if (TF_about_EI == null) {//GEN-END:|104-getter|0|104-preInit
            // write pre-init user code here
            TF_about_EI = new TextField(LocalizationSupport.getMessage("about"), "", 32, TextField.ANY);//GEN-LINE:|104-getter|1|104-postInit
            // write post-init user code here
        }//GEN-BEGIN:|104-getter|2|
        return TF_about_EI;
    }
    //</editor-fold>//GEN-END:|104-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: imageEditItem ">//GEN-BEGIN:|105-getter|0|105-preInit
    /**
     * Returns an initiliazed instance of imageEditItem component.
     * @return the initialized component instance
     */
    public ImageItem getImageEditItem() {
        if (imageEditItem == null) {//GEN-END:|105-getter|0|105-preInit
            // write pre-init user code here
            imageEditItem = new ImageItem("", getImage1(), ImageItem.LAYOUT_DEFAULT, "<Imagem perdida>");//GEN-LINE:|105-getter|1|105-postInit
            // write post-init user code here
        }//GEN-BEGIN:|105-getter|2|
        return imageEditItem;
    }
    //</editor-fold>//GEN-END:|105-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: TF_rgb_EI ">//GEN-BEGIN:|106-getter|0|106-preInit
    /**
     * Returns an initiliazed instance of TF_rgb_EI component.
     * @return the initialized component instance
     */
    public TextField getTF_rgb_EI() {
        if (TF_rgb_EI == null) {//GEN-END:|106-getter|0|106-preInit
            // write pre-init user code here
            TF_rgb_EI = new TextField("RGB:", "", 1440, TextField.ANY);//GEN-LINE:|106-getter|1|106-postInit
            // write post-init user code here
        }//GEN-BEGIN:|106-getter|2|
        return TF_rgb_EI;
    }
    //</editor-fold>//GEN-END:|106-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backDB_Import ">//GEN-BEGIN:|108-getter|0|108-preInit
    /**
     * Returns an initiliazed instance of backDB_Import component.
     * @return the initialized component instance
     */
    public Command getBackDB_Import() {
        if (backDB_Import == null) {//GEN-END:|108-getter|0|108-preInit
            // write pre-init user code here
            backDB_Import = new Command("Voltar", Command.BACK, 0);//GEN-LINE:|108-getter|1|108-postInit
            // write post-init user code here
        }//GEN-BEGIN:|108-getter|2|
        return backDB_Import;
    }
    //</editor-fold>//GEN-END:|108-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: importDB ">//GEN-BEGIN:|107-getter|0|107-preInit
    /**
     * Returns an initiliazed instance of importDB component.
     * @return the initialized component instance
     */
    public Form getImportDB() {
        if (importDB == null) {//GEN-END:|107-getter|0|107-preInit
            // write pre-init user code here
            importDB = new Form(LocalizationSupport.getMessage("import"), new Item[] { getTF_name_Import() });//GEN-BEGIN:|107-getter|1|107-postInit
            importDB.addCommand(getBackDB_Import());
            importDB.setCommandListener(this);//GEN-END:|107-getter|1|107-postInit
            // write post-init user code here
        }//GEN-BEGIN:|107-getter|2|
        return importDB;
    }
    //</editor-fold>//GEN-END:|107-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: TF_name_Import ">//GEN-BEGIN:|112-getter|0|112-preInit
    /**
     * Returns an initiliazed instance of TF_name_Import component.
     * @return the initialized component instance
     */
    public TextField getTF_name_Import() {
        if (TF_name_Import == null) {//GEN-END:|112-getter|0|112-preInit
            // write pre-init user code here
            TF_name_Import = new TextField(LocalizationSupport.getMessage("name"), "", 32, TextField.ANY);//GEN-LINE:|112-getter|1|112-postInit
            // write post-init user code here
        }//GEN-BEGIN:|112-getter|2|
        return TF_name_Import;
    }
    //</editor-fold>//GEN-END:|112-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemHISTO ">//GEN-BEGIN:|113-getter|0|113-preInit
    /**
     * Returns an initiliazed instance of itemHISTO component.
     * @return the initialized component instance
     */
    public Command getItemHISTO() {
        if (itemHISTO == null) {//GEN-END:|113-getter|0|113-preInit
            // write pre-init user code here
            itemHISTO = new Command("histo", Command.ITEM, 0);//GEN-LINE:|113-getter|1|113-postInit
            // write post-init user code here
        }//GEN-BEGIN:|113-getter|2|
        return itemHISTO;
    }
    //</editor-fold>//GEN-END:|113-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: SI_H_ADD ">//GEN-BEGIN:|116-getter|0|116-preInit
    /**
     * Returns an initiliazed instance of SI_H_ADD component.
     * @return the initialized component instance
     */
    public StringItem getSI_H_ADD() {
        if (SI_H_ADD == null) {//GEN-END:|116-getter|0|116-preInit
            // write pre-init user code here
            SI_H_ADD = new StringItem("", "pegue o HISTO primeiro!");//GEN-LINE:|116-getter|1|116-postInit
            // write post-init user code here
        }//GEN-BEGIN:|116-getter|2|
        return SI_H_ADD;
    }
    //</editor-fold>//GEN-END:|116-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: SI_H_Get ">//GEN-BEGIN:|117-getter|0|117-preInit
    /**
     * Returns an initiliazed instance of SI_H_Get component.
     * @return the initialized component instance
     */
    public StringItem getSI_H_Get() {
        if (SI_H_Get == null) {//GEN-END:|117-getter|0|117-preInit
            // write pre-init user code here
            SI_H_Get = new StringItem("", "");//GEN-BEGIN:|117-getter|1|117-postInit
            SI_H_Get.setLayout(ImageItem.LAYOUT_DEFAULT | ImageItem.LAYOUT_NEWLINE_AFTER);//GEN-END:|117-getter|1|117-postInit
            // write post-init user code here
        }//GEN-BEGIN:|117-getter|2|
        return SI_H_Get;
    }
    //</editor-fold>//GEN-END:|117-getter|2|



    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay () {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet ();
        } else {
            initialize ();
            startMIDlet ();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }

    /**Identifies the substrings in a given string that are delimited
    by one or more characters specified in an array, and then
    places the substrings into a String array.
    */
    public static String[] split(String strString, String strDelimiter)
    {
        String[] strArray;
        int iOccurrences = 0;
        int iIndexOfInnerString = 0;
        int iIndexOfDelimiter = 0;
        int iCounter = 0;

        //Check for null input strings.
        if (strString == null)
        {
            throw new IllegalArgumentException("Input string cannot be null.");
        }
        //Check for null or empty delimiter strings.
        if (strDelimiter.length() <= 0 || strDelimiter == null)
        {
            throw new IllegalArgumentException("Delimeter cannot be null or empty.");
        }

        //strString must be in this format: (without {} )
        //"{str[0]}{delimiter}str[1]}{delimiter} ...
        // {str[n-1]}{delimiter}{str[n]}{delimiter}"

        //If strString begins with delimiter then remove it in order
        //to comply with the desired format.

        if (strString.startsWith(strDelimiter))
        {
            strString = strString.substring(strDelimiter.length());
        }

        //If strString does not end with the delimiter then add it
        //to the string in order to comply with the desired format.
        if (!strString.endsWith(strDelimiter))
        {
            strString += strDelimiter;
        }

        //Count occurrences of the delimiter in the string.
        //Occurrences should be the same amount of inner strings.
        while((iIndexOfDelimiter = strString.indexOf(strDelimiter, iIndexOfInnerString)) != -1)
        {
            iOccurrences += 1;
            iIndexOfInnerString = iIndexOfDelimiter +
            strDelimiter.length();
        }

        //Declare the array with the correct size.
        strArray = new String[iOccurrences];

        //Reset the indices.
        iIndexOfInnerString = 0;
        iIndexOfDelimiter = 0;

        //Walk across the string again and this time add the
        //strings to the array.
        while((iIndexOfDelimiter = strString.indexOf(strDelimiter, iIndexOfInnerString)) != -1)
        {

            //Add string to array.
            strArray[iCounter] = strString.substring(iIndexOfInnerString,iIndexOfDelimiter);

            //Increment the index to the next character after
            //the next delimiter.
            iIndexOfInnerString = iIndexOfDelimiter + strDelimiter.length();

            //Inc the counter.
            iCounter += 1;
        }
    return strArray;
    }

    //verify and open camera
    private void verifyCamera()
    {
        String supports = System.getProperty("video.snapshot.encodings");
        if (supports != null && supports.length() > 0)
        {
            camera.append(LocalizationSupport.getMessage("ready"));
            showCamera();
        } else camera.append(LocalizationSupport.getMessage("cannot"));

        camera.setCommandListener(this);
    }

    private void showCamera()
    {
        try
        {
            mPlayer = Manager.createPlayer("capture://video");
            mPlayer.realize();

            mVideoControl = (VideoControl)mPlayer.getControl("VideoControl");

            Item item = (Item)mVideoControl.initDisplayMode(GUIControl.USE_GUI_PRIMITIVE, null);
            camera.append(item);

            mPlayer.start();
        }
        catch (IOException ioe) {System.out.println("IOException: "+ioe);}
        catch (MediaException me) {System.out.println("MediaException: "+me);}
    }

    //list all record stores in DB
    public void analyzeAll()
    {
        String[] names = RecordStore.listRecordStores();
        CG_DB.deleteAll();
        for( int i = 0;names != null && i < names.length; ++i )
        {
            open(names[i]);
            CG_DB.append(getRecord(1), null);
            close();
        }

        if (CG_DB.getSelectedIndex() != -1)
        {
            open(CG_DB.getString(CG_DB.getSelectedIndex()));
            try
            {
                SI_memory_DB.setText(String.valueOf(rs.getSizeAvailable()/1048576));
            } catch (RecordStoreNotOpenException ex) {System.out.println("RecordStoreNotOpenException: "+ex);}
            close();
        }
    }

    public void open(String RS_NAME1)
    {
        try
        {
                rs = RecordStore.openRecordStore(RS_NAME1, true);
        } catch (RecordStoreNotFoundException e) {System.out.println("RecordStoreNotFoundException: "+e);}
        catch (RecordStoreException e) {System.out.println("RecordStoreException: "+e);}
    }

    public void close()
    {
        try
        {
            rs.closeRecordStore();
        } catch( RecordStoreNotOpenException e ){System.out.println("RecordStoreNotOpenException: "+e);}
        catch( RecordStoreException e ){System.out.println("RecordStoreException: "+e);}
    }

    public String getRecord(int id)
    {
        String toReturn = "";
        try
        {
            byte[] data = rs.getRecord( id );
            toReturn = new String(data,0,data.length);

        }catch (ArrayIndexOutOfBoundsException e) {System.out.println("ArrayIndexOutOfBoundsException: "+e);}
        catch (InvalidRecordIDException e) {System.out.println("InvalidRecordIDException: "+e);}
        catch (RecordStoreNotOpenException e) {System.out.println("RecordStoreNotOpenException: "+e);}
        catch (RecordStoreException e) {System.out.println("RecordStoreException: "+e);}
        return toReturn;
    }

    public void capture()
    {
        try
        {
          // Get the image.
          byte[] raw = mVideoControl.getSnapshot(null);
          image = Image.createImage(raw, 0, raw.length);
          imageclip = Image.createImage(image, image.getHeight()/2 - 50, image.getWidth()/2 -50, 100, 100, 0);
          imageGet.setImage(imageclip);

          // Shut down the player.
          mPlayer.close();
          mPlayer = null;
          mVideoControl = null;
        }catch (MediaException me) {System.out.println("MediaException: "+me);}
    }

    private void getHISTO(Image imgOK, StringItem SI)
    {

        int Width = imgOK.getWidth();
        int Height = imgOK.getHeight();
        int i = 0;
        int num = Width * Height;
        int[] redstring = new int[num];
        int[] greenstring = new int[num];
        int[] bluestring = new int[num];
        int[] Gint = new int[num];
        int[] rgb = new int[num];
        double L = 0;
        double S = 0;
        
          imgOK.getRGB(rgb, 0, Width, 0, 0, Width, Height);

        int max1 = 0;
        int min1 = 0;
        int mm = 0;

        for (i = 0; i < num; i++)
        {
            Gint[i] = 0;
            redstring[i] = 0;
            greenstring[i] = 0;
            bluestring[i] = 0;

            redstring[i] = ((rgb[i] >> 16) & 0xff);
            greenstring[i] = ((rgb[i] >> 8) & 0xff);
            bluestring[i] = ((rgb[i]) & 0xff);
            redstring[i] = (int)(redstring[i]*0.39);
            greenstring[i] = (int)(greenstring[i]*0.39);
            bluestring[i] = (int)(bluestring[i]*0.39);
//            System.out.println("redstring["+i+"]: "+redstring[i]);
//            System.out.println("greenstring["+i+"]: "+greenstring[i]);
//            System.out.println("bluestring["+i+"]: "+bluestring[i]);
            
            if (redstring[i]>greenstring[i] && redstring[i]>bluestring[i]) max1 = redstring[i]; else
            if (greenstring[i]>redstring[i] && greenstring[i]>bluestring[i]) max1 = greenstring[i]; else
            max1 = bluestring[i];
            //System.out.println("max1: "+max1);

            if (redstring[i]<greenstring[i] && redstring[i]<bluestring[i]) min1 = redstring[i]; else
            if (greenstring[i]<redstring[i] && greenstring[i]<bluestring[i]) min1 = greenstring[i]; else
            min1 = bluestring[i];
            //System.out.println("min1: "+min1);

            mm = (max1 - min1);
            //System.out.println("mm: "+mm);

            if (max1 == min1) 
            {
                Gint[i] = 0;
                //System.out.println("max1 == min1 / "+max1+" == "+min1);
            }
                else if (max1 == redstring[i])
                {
                    Gint[i] = (int) 60*(greenstring[i]-bluestring[i])/mm;
                    //System.out.println("max1 == redstring["+i+"] / "+max1+" == "+redstring[i]);
                }
                else if (max1 == greenstring[i])
                {
                    Gint[i] = (int) 60*(2 + (bluestring[i]-redstring[i])/mm);
                    //System.out.println("max1 == greenstring["+i+"] / "+max1+" == "+greenstring[i]);
                }
                else
                {
                    Gint[i] = (int) 60*(4 + (redstring[i]-greenstring[i])/mm);
                    //System.out.println("max1 == bluestring["+i+"] / "+max1+" == "+bluestring[i]);
                }
            
            
            if (Gint[i] < 0) Gint[i] = Gint[i]+360;
            //System.out.println("Gint1[+"+i+"]: "+Gint[i]+"\n\n");

            L = (max1 + min1)/2;

            if (max1 == min1) S = 0;
            else if (L < 0.5) S = (max1 - min1)/(max1 + min1);
            else S = (max1 - min1)/(2 - max1 - min1);
            
            //simplification
            if (Gint[i] < 15 && Gint[i] >= 345) Gint[i] = 0;
            if (Gint[i] < 45 && Gint[i] >= 15) Gint[i] = 30;
            if (Gint[i] < 75 && Gint[i] >= 45) Gint[i] = 60;
            if (Gint[i] < 105 && Gint[i] >= 75) Gint[i] = 90;
            if (Gint[i] < 135 && Gint[i] >= 105) Gint[i] = 120;
            if (Gint[i] < 165 && Gint[i] >= 135) Gint[i] = 150;
            if (Gint[i] < 195 && Gint[i] >= 165) Gint[i] = 180;
            if (Gint[i] < 225 && Gint[i] >= 195) Gint[i] = 210;
            if (Gint[i] < 255 && Gint[i] >= 225) Gint[i] = 240;
            if (Gint[i] < 285 && Gint[i] >= 255) Gint[i] = 270;
            if (Gint[i] < 315 && Gint[i] >= 285) Gint[i] = 300;
            if (Gint[i] < 345 && Gint[i] >= 315) Gint[i] = 330;

            if (S < 0.25) S = 0;
            if (S < 0.5 &&  S >= 0.25) S = 0.375;
            if (S < 0.75 &&  S >= 0.5) S = 0.625;
            if (S < 1 &&  S >= 0.75) S = 0.875;

            if (L < 0.1) L = 0;
            if (L < 0.2 &&  L >= 0.1) L = 0.15;
            if (L < 0.3 &&  L >= 0.2) L = 0.25;
            if (L < 0.4 &&  L >= 0.3) L = 0.35;
            if (L < 0.5 &&  L >= 0.4) L = 0.45;
            if (L < 0.6 &&  L >= 0.5) L = 0.55;
            if (L < 0.7 &&  L >= 0.6) L = 0.65;
            if (L < 0.8 &&  L >= 0.7) L = 0.75;
            if (L < 0.9 &&  L >= 0.8) L = 0.85;
            if (L < 1 &&  L >= 0.9) L = 0.95;

            //HSL to RGB
            double r = 0;
            double g = 0;
            double b = 0;
            double h = Gint[i]/360;
            double temp1 =0;
            double temp2 = 0;
            double tempr = 0;
            double tempg = 0;
            double tempb = 0;

            if(S == 0) r = g = b = L;
            else
            {
                if(L < 0.5) temp2 = L * (1 + S);
                else temp2 = (L + S) - (L * S);
                temp1 = 2 * L - temp2;
                tempr = h + 1.0 / 3.0;
                if(tempr > 1) tempr--;
                tempg = h;
                tempb = h - 1.0 / 3.0;
                if(tempb < 0) tempb++;

                if(tempr < 1.0 / 6.0) r = temp1 + (temp2 - temp1) * 6.0 * tempr;
                else if(tempr < 0.5) r = temp2;
                else if(tempr < 2.0 / 3.0) r = temp1 + (temp2 - temp1) * ((2.0 / 3.0) - tempr) * 6.0;
                else r = temp1;

                if(tempg < 1.0 / 6.0) g = temp1 + (temp2 - temp1) * 6.0 * tempg;
                else if(tempg < 0.5) g = temp2;
                else if(tempg < 2.0 / 3.0) g = temp1 + (temp2 - temp1) * ((2.0 / 3.0) - tempg) * 6.0;
                else g = temp1;

                if(tempb < 1.0 / 6.0) b = temp1 + (temp2 - temp1) * 6.0 * tempb;
                else if(tempb < 0.5) b = temp2;
                else if(tempb < 2.0 / 3.0) b = temp1 + (temp2 - temp1) * ((2.0 / 3.0) - tempb) * 6.0;
                else b = temp1;
            }

//            redstring[i] = (int) r*255;
//            greenstring[i] = (int) g*255;
//            bluestring[i] = (int) b*255;
//            System.out.println("bluestring[+"+i+"]: "+bluestring[i]);
//            System.out.println("b: "+b);

            Gint[i] = (int) (0.3*r + 0.59*g + 0.11*b);
            System.out.println("Gint[+"+i+"]: "+Gint[i]+"\n");

            SI.setText("Simplification... "+(int)(i*100/num)+"%");


            

        }

//        //normalization
//        max1 = 0;
//        min1 = 360;
//        for (i = 0; i < num; i++)
//        {
//            if (Gint[i] < min1) min1 = Gint[i];
//            if (Gint[i] > max1) max1 = Gint[i];
//        }
//
//        for (i = 0; i < num; i++)
//        {
//            //System.out.println("max1: "+max1);
//            //System.out.println("min1: "+max1);
//            //System.out.println("Gint[+"+i+"]: "+Gint[i]);
//
//            if (max1 == min1) Gint[i] = 0; else
//            Gint[i] = (int)((Gint[i]-min1)*(360/(max1-min1)));
//
//            //System.out.println("Gint2[+"+i+"]: "+Gint[i]);
//
//            SI.setText("Normalização... "+(int)(i*100/num)+"%");
//        }
//
//        //equalization
//        for (i = 0; i < 360; i++)
//        {
//            frequencyG[i] = 0;
//        }
//
//        for (i = 0; i < num; i++)
//        {
//            frequencyG[Gint[i]] = frequencyG[Gint[i]] + 1;
//        }
//
//        int[] norm = new int[360];
//        for (i = 0; i < 360; i++)
//        {
//            norm[i] = 360*(frequencyG[i] - 1)/num;
//            System.out.println("norm["+i+"]:" + norm[i]);
//            System.out.println("frequencyG["+i+"]:" + frequencyG[i]);
//        }
//
//        int[] Gint2 = new int[num];
//        for (i = 0; i < num; i++)
//        {
//            Gint2[i] = norm[Gint[i]];
//            System.out.println("Gint2["+i+"]:" + Gint2[i]);
//            SI.setText("Equalizando... "+(int)(i*100/num)+"%");
//        }
//
//        for (i = 0; i < 360; i++)
//        {
//            frequencyG[i] = 0;
//        }
//
//        for (i = 0; i < num; i++)
//        {
//            frequencyG[Gint2[i]] = frequencyG[Gint2[i]] + 1;
//        }
//
//        //fill
//        int zeros = 0;
//        int max3 = 0;
//        int min3 = 0;
//        int a = 0;
//        int b = 0;
//        for (i = 1; i < 360; i++)
//        {
//            if (frequencyG[i] == 0) zeros++;
//            else if (frequencyG[i] != 0 && zeros == 0) min3 = frequencyG[i];
//            else //if (frequencyG[i] != 0 && frequencyG[i - 1] == 0)
//            {
//                max3 = frequencyG[i];
//                b = zeros;
//                for (a = 1; a<=zeros; a++)
//                {
//                    frequencyG[i-b] = min3 + (int) (a*((max3-min3)/(zeros+1)));
//                    b--;
//                }
//                zeros = 0;
//            }
//            System.out.println("max3: "+max3);
//            System.out.println("min3: "+min3);
//            SI.setText("Preenchendo... "+(int)(i*0.277)+"%");
//        }


        //histogram
        for (i = 0; i < 360; i++)
        {
            frequencyG[i] = 0;
        }

        for (i = 0; i < num; i++)
        {
            frequencyG[Gint[i]] = frequencyG[Gint[i]] + 1;
        }

        frequency = "";
        for (i = 0; i<360;i++)
        {
            frequency = frequency + String.valueOf(frequencyG[i]) + ";";
            SI.setText("Histograma... " + (int)(i*0.277)+"%");
        }
        SI.setText("Pronto!");

    }

    

    private void compare()
    {
        String temp = "";
        String[] temp2;
        String temp3 = "";
        String[] temp4;

        try
        {
            x = rs.getNumRecords();
        } catch (RecordStoreNotOpenException ex) {System.out.println("RecordStoreNotOpenException: "+ex);}
        //System.out.println("x: "+x+"\n");

        CG_Get.deleteAll();

        for (int y = 4; y<=x; y++)
        {
            temp = getRecord(y);
            //System.out.println("getRecord("+y+"): "+getRecord(y)+"\n");

            temp2 = split(temp,"\n");
            temp3 = temp2[1];

            temp4 = split(temp3,";");

            double Xrgb = 0;
            double a = 0;
            double b = 0;
            double c = 0;
            int z = 0;

            for (int i = 0; i<360;i++)
            {
                z = Integer.parseInt(temp4[i]);
                
                a = (frequencyG[i] - z)*(frequencyG[i] - z);
                b = (frequencyG[i] + z);
                if (a == 0)
                    c = 0;
                    else if (b == 0)
                    c = 0;
                    else  c = a/b;

                Xrgb = Xrgb + c;
            }
            //System.out.println("Xrgb: "+Xrgb);

            // Xrgb (prob ; 999)
            double x2 = 0;
            if (Xrgb <897.96) x2 = 99; else
            if (Xrgb <909.36) x2 = 98; else
            if (Xrgb <916.64) x2 = 97; else
            if (Xrgb <922.14) x2 = 96; else
            if (Xrgb <926.63) x2 = 95; else
            if (Xrgb <930.47) x2 = 94; else
            if (Xrgb <933.84) x2 = 93; else
            if (Xrgb <936.86) x2 = 92; else
            if (Xrgb <939.62) x2 = 91; else
            if (Xrgb <942.16) x2 = 90; else
            if (Xrgb <944.53) x2 = 89; else
            if (Xrgb <946.75) x2 = 88; else
            if (Xrgb <948.85) x2 = 87; else
            if (Xrgb <950.84) x2 = 86; else
            if (Xrgb <952.74) x2 = 85; else
            if (Xrgb <954.56) x2 = 84; else
            if (Xrgb <956.3) x2 = 83; else
            if (Xrgb <957.99) x2 = 82; else
            if (Xrgb <959.62) x2 = 81; else
            if (Xrgb <961.2) x2 = 80; else
            if (Xrgb <962.73) x2 = 79; else
            if (Xrgb <964.23) x2 = 78; else
            if (Xrgb <965.68) x2 = 77; else
            if (Xrgb <967.11) x2 = 76; else
            if (Xrgb <968.5) x2 = 75; else
            if (Xrgb <969.86) x2 = 74; else
            if (Xrgb <971.2) x2 = 73; else
            if (Xrgb <972.52) x2 = 72; else
            if (Xrgb <973.81) x2 = 71; else
            if (Xrgb <975.09) x2 = 70; else
            if (Xrgb <976.34) x2 = 69; else
            if (Xrgb <977.58) x2 = 68; else
            if (Xrgb <978.81) x2 = 67; else
            if (Xrgb <980.02) x2 = 66; else
            if (Xrgb <981.22) x2 = 65; else
            if (Xrgb <982.4) x2 = 64; else
            if (Xrgb <983.58) x2 = 63; else
            if (Xrgb <984.75) x2 = 62; else
            if (Xrgb <985.9) x2 = 61; else
            if (Xrgb <987.06) x2 = 60; else
            if (Xrgb <988.2) x2 = 59; else
            if (Xrgb <989.34) x2 = 58; else
            if (Xrgb <990.47) x2 = 57; else
            if (Xrgb <991.6) x2 = 56; else
            if (Xrgb <992.73) x2 = 55; else
            if (Xrgb <993.85) x2 = 54; else
            if (Xrgb <994.97) x2 = 53; else
            if (Xrgb <996.09) x2 = 52; else
            if (Xrgb <997.21) x2 = 51; else
            if (Xrgb <998.33) x2 = 50; else
            if (Xrgb <999.45) x2 = 49; else
            if (Xrgb <1000.58) x2 = 48; else
            if (Xrgb <1001.7) x2 = 47; else
            if (Xrgb <1002.83) x2 = 46; else
            if (Xrgb <1003.96) x2 = 45; else
            if (Xrgb <1005.09) x2 = 44; else
            if (Xrgb <1006.23) x2 = 43; else
            if (Xrgb <1007.38) x2 = 42; else
            if (Xrgb <1008.54) x2 = 41; else
            if (Xrgb <1009.7) x2 = 40; else
            if (Xrgb <1010.87) x2 = 39; else
            if (Xrgb <1012.05) x2 = 38; else
            if (Xrgb <1013.23) x2 = 37; else
            if (Xrgb <1014.44) x2 = 36; else
            if (Xrgb <1015.65) x2 = 35; else
            if (Xrgb <1016.88) x2 = 34; else
            if (Xrgb <1018.12) x2 = 33; else
            if (Xrgb <1019.38) x2 = 32; else
            if (Xrgb <1020.65) x2 = 31; else
            if (Xrgb <1021.95) x2 = 30; else
            if (Xrgb <1023.26) x2 = 29; else
            if (Xrgb <1024.6) x2 = 28; else
            if (Xrgb <1025.97) x2 = 27; else
            if (Xrgb <1027.36) x2 = 26; else
            if (Xrgb <1028.77) x2 = 25; else
            if (Xrgb <1030.23) x2 = 24; else
            if (Xrgb <1031.71) x2 = 23; else
            if (Xrgb <1033.23) x2 = 22; else
            if (Xrgb <1034.8) x2 = 21; else
            if (Xrgb <1036.41) x2 = 20; else
            if (Xrgb <1038.07) x2 = 19; else
            if (Xrgb <1039.79) x2 = 18; else
            if (Xrgb <1041.58) x2 = 17; else
            if (Xrgb <1043.43) x2 = 16; else
            if (Xrgb <1045.36) x2 = 15; else
            if (Xrgb <1047.38) x2 = 14; else
            if (Xrgb <1049.51) x2 = 13; else
            if (Xrgb <1051.76) x2 = 12; else
            if (Xrgb <1054.14) x2 = 11; else
            if (Xrgb <1056.7) x2 = 10; else
            if (Xrgb <1059.44) x2 = 9; else
            if (Xrgb <1062.44) x2 = 8; else
            if (Xrgb <1065.73) x2 = 7; else
            if (Xrgb <1069.42) x2 = 6; else
            if (Xrgb <1073.64) x2 = 5; else
            if (Xrgb <1078.61) x2 = 4; else
            if (Xrgb <1084.74) x2 = 3; else
            if (Xrgb <1092.93) x2 = 2; else
            if (Xrgb <1105.92) x2 = 1; else
            x2 = 0;

            if (x2>0)
            CG_Get.append(temp2[0]+" > "+x2+"% x2: "+ (int) Xrgb,null);
            else
            CG_Get.append(temp2[0]+" < 1% x2: "+ (int) Xrgb,null);
        }
    }

    private void additemCG(ChoiceGroup CG)
    {
        x = 0;
        String temp = "";
        String[] temp2;
        try
        {
            x = rs.getNumRecords();
        } catch (RecordStoreNotOpenException ex) {System.out.println("RecordStoreNotOpenException: "+ex);}
        //System.out.println("x: "+x);

        CG.deleteAll();

        for (int y = 4; y<=x; y++)
        {
            temp = getRecord(y);
            //System.out.println("getRecord("+y+"): "+getRecord(y)+"\n");

            temp2 = split(temp,"\n");
            CG.append(temp2[0],null);
        }
    }

    private void add(String nome)
    {
        id = 0;
        try
         {
            id = rs.getNextRecordID();
         } catch (RecordStoreException ex) {System.out.println("RecordStoreException: "+ex);}

        try
        {
            byte[] data = nome.getBytes();
            id = rs.addRecord(data, 0, data.length);
        } catch( RecordStoreException e ){System.out.println("RecordStoreException: "+e);}
    }
    
    public void set(String nome, int id)
    {
        try
        {
            byte[] data = nome.getBytes();
            rs.setRecord(id, data, 0, data.length);
        }catch (RecordStoreFullException e) {System.out.println("RecordStoreFullException: "+e);}
        catch( RecordStoreNotOpenException e ){System.out.println("RecordStoreNotOpenException: "+e);}
        catch( RecordStoreException e ){System.out.println("RecordStoreException: "+e);}
    }

    private void getImageX(int ID, ImageItem imageX)
    {
        String temp = null;
        temp = getRecord(ID);
        String[] temp2;
        temp2 = split(temp,"\n");
        try
        {
            String imagename = null;
            byte[] b = null;
            b = temp2[2].getBytes();
            ByteArrayInputStream  bin = new ByteArrayInputStream( b );
            DataInputStream din = new DataInputStream( bin );
            imagename = din.readUTF();
            int remaining = (b.length-imagename.getBytes().length-2)/4;
            int[] rawdata = new int[remaining];
            for(int k =0 ;k<rawdata.length ;k++)
            {
                rawdata[k] = din.readInt();
            }
            imageclip = Image.createRGBImage(rawdata,100, 100, false);
            bin.reset();
            din.close();
            din =null;
        }catch (IOException e) {System.out.println("IOException: "+e);}
        imageX.setImage(imageclip);

    }

    private void export(String url1)
    {

    RS_NAME = CG_DB.getString(CG_DB.getSelectedIndex());
    open(RS_NAME);
    try
    {

        try
        {
              x = rs.getNumRecords();
        } catch (RecordStoreNotOpenException ex) {System.out.println("RecordStoreNotOpenException: "+ex);}

        System.out.println("rs.getNumRecords(): "+x);

        String tempfile = "";
        tempfile = getRecord(3);
        String[] tempfile2 = split(tempfile,"\t");
        System.out.println("tempfile2[0]: "+tempfile2[0]);
        System.out.println("tempfile2[1]: "+tempfile2[1]);
        tempfile2[1] = String.valueOf(x);
        set(tempfile2[0]+"\t"+tempfile2[1],3);

        String exp = "";
        for (int i = 1; i <= x; i++)
        {
            exp = exp + getRecord(i) + "\n!!\n";
            System.out.println("getRecord("+i+"): "+getRecord(i));
        }

        byte data[] = exp.getBytes();
        FileConnection fconn = (FileConnection)Connector.open(url1, Connector.READ_WRITE);
        if (!fconn.exists())
        {
            fconn.create();
        }
        OutputStream ops = fconn.openOutputStream();
        ops.write(data);
        ops.close();
        fconn.close();

    }
    catch (IOException ioe) {System.out.println("IOException: "+ioe);}
    catch (SecurityException se){System.out.println("SecurityException: "+se);}
    close();
    }

    private void importfile(String filename) throws IOException
    {

    FileConnection sc = (FileConnection)Connector.open(filename);
    InputStream is = sc.openInputStream();
    StringBuffer str = new StringBuffer();
    byte c;
    while ((c = (byte)is.read()) != -1)
    {
        str.append((char)c);
    }
    String tempfile = "";
    tempfile = str.toString();
    System.out.println("tempfile: " + tempfile);
    is.close();

    String[] tempfile2 = split(tempfile,"\n!!\n");
    RS_NAME = tempfile2[0];
    tempfile = "";
    tempfile = tempfile2[2];
    String[] tempfile3 = split(tempfile,"\t");
    int num2 = Integer.parseInt(tempfile3[1]);
    System.out.println("num2: " + num2);
    open(RS_NAME);
    for (int i = 0; i < num2; i++)
        {
            System.out.println("temp2["+i+"]: " + tempfile2[i]);
            add(tempfile2[i]);

        }
    close();
    }

}
